﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CONCURSO.EN;
using MJM.CBD.Conn;


namespace MJM.CONCURSO.AD
{
    public static class ConcursoAD
    {
        public static int Insertar(ConcursoEN obj)
        {
            int Codigo = 0;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_CONVEC_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_inCANGAN", obj.CantidadGanadores));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chFECINI", obj.FechaInicio));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chFECFIN", obj.FechaFin));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chHORINI", obj.HoraInicio));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chHORFIN", obj.HoraFin));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chTITCON", obj.Titulo));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chPRECON", obj.Premio));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chFECSOR", obj.FechaSorteo));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_chHORSOR", obj.HoraSorteo));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_inFLAELI",obj.Existe));
                SqlParameter prmID = new SqlParameter();
                prmID.Direction = ParameterDirection.ReturnValue;
                prmID.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prmID);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                Codigo = Convert.ToInt32(prmID.Value);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                Codigo = 0;
            }
            return Codigo;
        }

        public static int ActualizaEstado(ConcursoEN obj)
        {
            int Codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_CONVEC_ActualizaEstado", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.ID));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_inFLAELI", obj.Existe));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                Codigo = 0;
            }
            return Codigo;
        }

        public static List<ConcursoEN> Listar(ConcursoEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_CONVEC_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            List<ConcursoEN> lst = new List<ConcursoEN>();
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_inFLAELI", obj.Existe));
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var objConcurso = new ConcursoEN();
                    objConcurso.ID = dr.GetInt32(dr.GetOrdinal("CONVEC_P_inID"));
                    objConcurso.CantidadGanadores = dr.GetInt32(dr.GetOrdinal("CONVEC_inCANGAN"));
                    objConcurso.Existe = dr.GetInt32(dr.GetOrdinal("CONVEC_inFLAELI"));
                    objConcurso.FechaFin = dr.GetString(dr.GetOrdinal("CONVEC_chFECFIN"));
                    objConcurso.FechaInicio = dr.GetString(dr.GetOrdinal("CONVEC_chFECINI"));
                    objConcurso.FechaSorteo = dr.GetString(dr.GetOrdinal("CONVEC_chFECSOR"));
                    objConcurso.HoraFin = dr.GetString(dr.GetOrdinal("CONVEC_chHORFIN"));
                    objConcurso.HoraInicio = dr.GetString(dr.GetOrdinal("CONVEC_chHORINI"));
                    objConcurso.HoraSorteo = dr.GetString(dr.GetOrdinal("CONVEC_chHORSOR"));
                    objConcurso.Premio = dr.GetString(dr.GetOrdinal("CONVEC_chPRECON"));
                    objConcurso.Titulo = dr.GetString(dr.GetOrdinal("CONVEC_chTITCON"));
                    lst.Add(objConcurso);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }
    }
}
